package com.atguigu.gmail.item.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.atguigu.gmail.api.bean.PmsSkuInfo;
import com.atguigu.gmail.api.bean.PmsSkuSaleAttrValue;
import com.atguigu.gmail.api.service.SkuService;
import com.atguigu.gmail.api.bean.PmsProductSaleAttr;
import com.atguigu.gmail.api.service.SpuService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Controller
public class ItemController {

    @Reference
    SkuService skuService;
    @Reference
    SpuService spuService;

    @RequestMapping("{skuId}.html")
    public  String item(@PathVariable String skuId, ModelMap modelMap){
        PmsSkuInfo pmsSkuInfo = skuService.getSkuById(skuId);
        modelMap.put("skuInfo", pmsSkuInfo);


        //销售属性列表
        List<PmsProductSaleAttr> pmsProductSaleAttrs = spuService.spuSaleAttrListCheckBySku(pmsSkuInfo.getProductId(), pmsSkuInfo.getId());
        modelMap.put("spuSaleAttrListCheckBySku",pmsProductSaleAttrs);

        //查询当前sku的spu的其他sku集合的hash表
        Map<String, String> hashMap = new HashMap<>();
        List<PmsSkuInfo> pmsSkuInfos = skuService.getSkuSaleAttrValueListBySpu(pmsSkuInfo.getProductId());

        for (PmsSkuInfo skuInfo : pmsSkuInfos) {
            String k = "";
            String v = skuInfo.getId();

            List<PmsSkuSaleAttrValue> skuSaleAttrValueList = skuInfo.getSkuSaleAttrValueList();
            for (PmsSkuSaleAttrValue pmsSkuSaleAttrValue : skuSaleAttrValueList) {
                String saleAttrValueId = pmsSkuSaleAttrValue.getSaleAttrValueId();

                k += saleAttrValueId + "|";
            }

            hashMap.put(k, v);

        }
        //使用fastjson提供的方法将json转化为字符串传到前端
        String s = JSON.toJSONString(hashMap);
        modelMap.put("skuSaleAttrHashJsonStr",s);

        return "item";
    }


    @RequestMapping("index")
    public String index(ModelMap modelMap){

        List<String> list = new ArrayList<>();
        for (int i=0; i<5; i++){
            list.add("循环数据"+i);
        }

        modelMap.put("hello", "hello thymeleaf");
        modelMap.put("list", list);
        modelMap.put("check", "1");
        return "index";
    }
}
